package com.zonski.jbots.desktop.applet;

import com.zonski.jbots.desktop.client.PainterCanvas;
import com.zonski.jbots.desktop.client.RoomRenderer;
import com.zonski.jbots.engine.FileInputStreamFactory;
import com.zonski.jbots.desktop.engine.EngineRunner;
import com.zonski.jbots.desktop.engine.InputMindKeyListener;
import com.zonski.jbots.desktop.engine.MinimalInputMindKeyListener;
import com.zonski.jbots.desktop.game.JBotsPanel;
import com.zonski.jbots.desktop.game.JBotsRoomRendererFactory;
import com.zonski.jbots.engine.Engine;
import com.zonski.jbots.engine.RoomFactory;
import com.zonski.jbots.game.JBotsEntityFactory;
import com.zonski.jbots.game.JBotsGameController;
import com.zonski.jbots.game.JBotsConstants;
import com.zonski.jbots.game.entity.InputMind;
import com.zonski.jbots.game.entity.BadBotMind;
import com.zonski.jbots.game.entity.MinimalInputMind;
import com.zonski.jbots.game.room.AsciiRoomFactory;

import java.applet.Applet;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 28, 2003
 * Time: 12:38:33 PM
 * To change this template use Options | File Templates.
 */
public class JBotsApplet extends Applet
{
    private EngineRunner runner;
    private boolean wasRunning;

    public JBotsApplet()
    {

    }

    public void init()
    {
        super.init();
        AppletDevice device = new AppletDevice(this, "sounds/", ".au");

        Engine engine = new Engine(new JBotsEntityFactory(device), device);

        RoomFactory roomFactory = new AsciiRoomFactory(
            new FileInputStreamFactory(
                "/data/levels/", "lvl"
            )
        );
        JBotsGameController controller = new JBotsGameController(engine, roomFactory);
        this.runner = new EngineRunner(controller, device);
        controller.setEngineRunner(runner);
        controller.setMaxLevel(9);
        PainterCanvas canvas = new PainterCanvas(controller);
        controller.setMode(JBotsGameController.LOADING);

        device.setCanvas(canvas);
        runner.setCanvas(canvas);
        createPlayer(engine, canvas, 0);
        createBot(engine, 1);
        createBot(engine, 2);
        createBot(engine, 3);

        JBotsPanel panel = new JBotsPanel();
        canvas.setSize(240, 200);
        panel.addCentrePanel(canvas);

        this.setLayout(new BorderLayout());
        this.add(panel, BorderLayout.CENTER);

        // give the images as much time as possible to load by doing
        // this last
        JBotsRoomRendererFactory roomRendererFactory;
        roomRendererFactory = new JBotsRoomRendererFactory(canvas);
        RoomRenderer roomRenderer = roomRendererFactory.createRoomRenderer();
        canvas.setRoomRenderer(roomRenderer);

        canvas.requestFocus();
        controller.setMode(JBotsGameController.DEMO);
    }

    public void start()
    {
        super.start();
        if(wasRunning && !runner.isRunning())
        {
            runner.start();
        }
    }

    public void stop()
    {
        super.stop();
        if(runner.isRunning())
        {
            runner.stop();
            wasRunning = true;
        }else{
            wasRunning = false;
        }
    }

    public void destroy()
    {
        super.destroy();
        this.removeAll();
    }

    private static void createPlayer(Engine engine, Component component, int id)
    {
        MinimalInputMind mind = new MinimalInputMind();
        MinimalInputMindKeyListener listener = new MinimalInputMindKeyListener(mind);
        engine.setMind(Engine.getPlayerMindName(id), mind);
        component.addKeyListener(listener);
    }

    private static void createBot(Engine engine, int id)
    {
        BadBotMind mind = new BadBotMind(JBotsConstants.BOMB_BOT);
        //TempRockMind mind = new TempRockMind();
        engine.setMind(Engine.getPlayerMindName(id), mind);
    }
}
